package com.it.park.controller.admin;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.it.park.client.service.IntegralIncomeService;
import com.it.park.client.service.ResouceFileService;
import com.it.park.common.JsonObj;
import com.it.park.common.Page;
import com.it.park.constant.Constant;
import com.it.park.model.IntegralIncome;
import com.it.park.model.ResourceFile;
import com.it.park.rms.client.model.User;
import com.it.park.rms.client.service.UserService;

/**
 * 资源上传
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/admin/resource")
public class AdminResourceController {
	private String rootPath=Constant.ADMIN_PATH;
	
	private Page page = null;
	@Autowired
	private ResouceFileService resouceFileService;
	@Autowired
	private IntegralIncomeService incomeService;
	@Autowired
	private UserService userService;
	
	@RequestMapping("resourceList")
	public String resourceList(HttpServletRequest request){
			return rootPath+"resource/resourceList";
	}
	
	@RequestMapping("listRows")
	@ResponseBody
	public Map<String,Object> listRows(HttpServletRequest request){
		int pageNow = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));
		Map<String,Object> map_ = new HashMap<>();
		Integer total = resouceFileService.selectTotal(map_);
		if(page != null){
			page = new Page(pageNow,total,rows);
		}else{
			page = new Page(1,total,rows);
		}
		map_.put("startPos", page.getStartPos());
		map_.put("pageSize", page.getPageSize());
		List<ResourceFile> list = resouceFileService.selectPagination(map_);
		Map<String,Object> map  =new HashMap<>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}
	
	/**
	 * 删除文件
	 */
	@RequestMapping("deleteById")
	@ResponseBody
	public JsonObj<String> deleteById(Integer id,HttpServletRequest request){
		JsonObj<String> jsonObj = new JsonObj<>();
		boolean success = resouceFileService.deleteByPrimaryKey(id);
		if(success){
			jsonObj.setMessage("删除成功!");
		}else{
			jsonObj.setSuccess(false);
			jsonObj.setMessage("删除失败!");
		}
			return jsonObj;
	}
	
	/**
	 * 添加文件
	 */
	@RequestMapping("save")
	@ResponseBody
	public JsonObj<String> save(ResourceFile resourceFile,HttpServletRequest request){
		JsonObj<String> json = new JsonObj<>();
			boolean success = resouceFileService.insertSelective(resourceFile);
			if(success){
				json.setSuccess(true);
				json.setMessage("添加成功!");
			}else{
				json.setSuccess(false);
				json.setMessage("添加失败!");
			}
			return json;
	}
	
	/**
	 * 修改文件
	 */
	@RequestMapping("edit")
	@ResponseBody
	public JsonObj<String> edit(ResourceFile resourceFile,HttpServletRequest request){
		JsonObj<String> json = new JsonObj<>();
		boolean success = resouceFileService.updateByPrimaryKeySelective(resourceFile);
		if(success){
			json.setSuccess(true);
			json.setMessage("修改成功!");
		}else{
			json.setSuccess(false);
			json.setMessage("修改失败!");
		}
		return json;
	}
	
	/**
	 * 修改文件状态
	 */
	@RequestMapping("editFilePass")
	@ResponseBody
	public JsonObj<String> editFilePass(@RequestParam("ids")String ids,HttpServletRequest request){
		JsonObj<String> json = new JsonObj<>();
		try {
			for (String id : ids.split(",")) {
				ResourceFile resourceFile = resouceFileService.selectByPrimaryKey(Integer.parseInt(id));
				resourceFile.setIsPass(1);
				resouceFileService.updateByPrimaryKeySelective(resourceFile);
				//积分收入增加记录
				IntegralIncome income = new IntegralIncome();
				income.setCreateDate(new Date());
				income.setIntegral(resourceFile.getScore());
				income.setUserId(resourceFile.getId());
				income.setSource(0);
				incomeService.insertSelective(income);
				//user表增加积分
				User u = userService.selectByPrimaryKey(resourceFile.getUserId());
				u.setIntegral(u.getIntegral()+resourceFile.getScore());
				userService.updateByPrimaryKeySelective(u);
			}
			json.setSuccess(true);
			json.setMessage("修改成功!");
		} catch (Exception e) {
			e.printStackTrace();
			json.setSuccess(false);
			json.setMessage("修改失败!");
		}
		return json;
	}
	
	
}
